欧美一区2区三区4区公司二百,国产精品婷婷午夜在线观看,自拍偷拍亚洲精品,国产美女诱惑一区二区

c++ c#結(jié)構(gòu)體調(diào)用

1、最近做項(xiàng)目遇到,C#調(diào)用C++dll里的函數(shù)需要傳遞結(jié)構(gòu)體參數(shù),發(fā)現(xiàn)這個(gè)并不是簡單的在C#里定義相應(yīng)的結(jié)構(gòu)體就可以的,下面以一個(gè)例子來說明解決的辦法,C++中的函數(shù)和結(jié)構(gòu)體如下:

uint msec_set_igr_gen_cfg(int port, IGR_GEN_T *igr_gen)
{

return 0;
}

typedef struct {
? int aa_disable; /*/< authentiation adjust checking disable */
? int badtag_rej; /*/< reject packet if it is bypassed due to badtag */
? int pad_en; /*/< pad non-rejected packets up to 64B */
? int byp_ctl_sl; /*/< bypass packet if SL field does not correspond to packet len */
? int byp_ctl_v; /*/< bypass packet if V bit is set */
? int byp_ctl_sc; /*/< bypass packet if SC bit and either ES or SCB bits are set */
? int byp_ctl_ec; /*/< bypass packet if DC bits are not 00 or 11 */
? int sectag_flag; /*/< select which flag bit indicates that a SEC tag was present in pkt */
} IGR_GEN_T;

在C#中 首先需要使用Dllimport將相應(yīng)的C++dll load進(jìn)來,然后定義相應(yīng)的結(jié)構(gòu)體,具體如下:

? ? ? ? [DllImport("..\\debug\\mgd_MacSec.dll")]
? ? ? ? private static extern UInt32 msec_set_igr_gen_cfg(int port, IntPtr igr_gen);

??

? ? ? ? 結(jié)構(gòu)體定義:

? ? [StructLayout(LayoutKind.Sequential)]
? ? ?public class IGR_GEN_T?
? ? ?{
? ? ? ? ?int aa_disable; /*/< authentiation adjust checking disable */
? ? ? ? ?int badtag_rej; /*/< reject packet if it is bypassed due to badtag */
? ? ? ? ?int pad_en; /*/< pad non-rejected packets up to 64B */
? ? ? ? ?int byp_ctl_sl; /*/< bypass packet if SL field does not correspond to packet len */
? ? ? ? ?int byp_ctl_v; /*/< bypass packet if V bit is set */
? ? ? ? ?int byp_ctl_sc; /*/< bypass packet if SC bit and either ES or SCB bits are set */
? ? ? ? ?int byp_ctl_ec; /*/< bypass packet if DC bits are not 00 or 11 */
? ? ? ? ?int sectag_flag; /*/< select which flag bit indicates that a SEC tag was present in pkt */
? ? ? ? ?public IGR_GEN_T()
? ? ? ? ?{
? ? ? ? ? ? ?aa_disable = 0;
? ? ? ? ? ? ?badtag_rej = 0;
? ? ? ? ? ? ?pad_en = 0;
? ? ? ? ? ? ?byp_ctl_ec = 0;
? ? ? ? ? ? ?byp_ctl_sc = 0;
? ? ? ? ? ? ?byp_ctl_sl = 0;
? ? ? ? ? ? ?byp_ctl_v = 0;
? ? ? ? ? ? ?sectag_flag = 0;
? ? ? ? ?}
? ? ?} ;

? 在代碼中具體引用函數(shù)時(shí)如下所示,

? ? ?IntPtr ptr = Marshal.AllocHGlobal(Marshal.SizeOf(igr_gen));
? ? ? ? ? ? ?Marshal.StructureToPtr(igr_gen, ptr, false);
? ? ? ? ? ? ?UInt32 ret = _msec_set_igr_gen_cfg(port, ptr);
? ? ? ? ? ? ?igr_gen = (IGR_GEN_T)Marshal.PtrToStructure(ptr, typeof(IGR_GEN_T));
? ? ? ? ? ? ?Marshal.FreeHGlobal(ptr);
? ? ? ? ? ? ?return ret;

從以上步驟可以看出,結(jié)構(gòu)體參數(shù)的傳遞需要marshal做輔助做相應(yīng)的轉(zhuǎn)化,以intptr的方式傳輸結(jié)構(gòu)體參數(shù)。

?

文章鏈接: http://www.qzkangyuan.com/21890.html

文章標(biāo)題:c++ c#結(jié)構(gòu)體調(diào)用

文章版權(quán):夢(mèng)飛科技所發(fā)布的內(nèi)容,部分為原創(chuàng)文章,轉(zhuǎn)載請(qǐng)注明來源,網(wǎng)絡(luò)轉(zhuǎn)載文章如有侵權(quán)請(qǐng)聯(lián)系我們!

聲明:本站所有文章,如無特殊說明或標(biāo)注,均為本站原創(chuàng)發(fā)布。任何個(gè)人或組織,在未征得本站同意時(shí),禁止復(fù)制、盜用、采集、發(fā)布本站內(nèi)容到任何網(wǎng)站、書籍等各類媒體平臺(tái)。如若本站內(nèi)容侵犯了原著者的合法權(quán)益,可聯(lián)系我們進(jìn)行處理。

給TA打賞
共{{data.count}}人
人已打賞
建站教程

關(guān)于C++結(jié)構(gòu)體的函數(shù)調(diào)用

2023-7-7 14:13:02

建站教程

C#調(diào)用

2023-7-7 14:44:32

0 條回復(fù) A文章作者 M管理員
    暫無討論,說說你的看法吧
?
個(gè)人中心
購物車
優(yōu)惠劵
今日簽到
有新私信 私信列表
搜索
主站蜘蛛池模板: 拜城县| 阿拉善右旗| 健康| 蓬安县| 鄢陵县| 墨竹工卡县| 呼伦贝尔市| 中宁县| 乌拉特前旗| 肃宁县| 莎车县| 宜阳县| 清新县| 泰宁县| 华阴市| 石城县| 白朗县| 杨浦区| 自贡市| 道真| 漳州市| 万盛区| 通州区| 集安市| 望江县| 岳西县| 绥阳县| 雅江县| 大同市| 新民市| 辽宁省| 托里县| 吴桥县| 会东县| 靖西县| 休宁县| 吴忠市| 浦北县| 阜宁县| 泸水县| 澄江县|